package com.fundynamic.codekata;

public class Game {

	private int rolls[] = new int[21];
	private int roll = 0;
	
	public void roll(int pins) {
		rolls[roll] = pins;
		roll++;
	}

	public boolean isStrike(int rollIndex) {
		return rolls[rollIndex] == 10;
	}
	
	public boolean isSpare(int rollIndex) {
		return (rolls[rollIndex] + rolls[rollIndex + 1]) == 10;
	}
	
	public int score() {
		int totalScore = 0;
		int rollIndex = 0;
		for (int frame = 0; frame < 10; frame++) {
			int frameScore = 0;
			
			if (isStrike(rollIndex)) {
				frameScore += 10;
				frameScore += rolls[rollIndex + 1];
				frameScore += rolls[rollIndex + 2];
				rollIndex++;
			} else if (isSpare(rollIndex)) {
				frameScore += 10;
				frameScore += rolls[rollIndex + 2];				
				rollIndex += 2;
			} else {
				frameScore += rolls[rollIndex] + rolls[rollIndex + 1];
				rollIndex += 2;
			}
			
			totalScore += frameScore;
		}
		
		return totalScore;
	}

	
	
}
